package com.cuiweiyou.bluemesh.ui;

import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;

import com.cuiweiyou.bluemesh.model.LightModel;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

public class GroupCommAdapter extends FragmentPagerAdapter {
    List<GroupCommFragment> fragmentList;
    List<String> groupNameList;

    public GroupCommAdapter(FragmentManager fm) {
        super(fm);

        fragmentList = new ArrayList<>();
        groupNameList = new ArrayList<>();
    }

    public void setGroup(Map<String, List<LightModel>> deviceMap,OnBluetoothProcessingListener listener) {
        Iterator<String> iterator = deviceMap.keySet().iterator();
        while (iterator.hasNext()){
            String group = iterator.next();
            List<LightModel> list = deviceMap.get(group);

            groupNameList.add(group);
            GroupCommFragment fragment = GroupCommFragment.newInstance(group, list,listener);
            fragmentList.add(fragment);
        }

        notifyDataSetChanged();
    }

    @Override
    public Fragment getItem(int position) {
        return null == fragmentList ? null : fragmentList.get(position);
    }

    @Override
    public int getCount() {
        return null == fragmentList ? 0 : fragmentList.size();
    }

    @Override
    public CharSequence getPageTitle(int position) {
        return null == groupNameList ? null : groupNameList.get(position);
    }
}